package com.csxy.six.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.six.R;
import com.csxy.six.entity.Announcement;
import com.csxy.six.service.AnnouncementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/annoservice/announcement")
@Tag(name = "公告管理", description = "提供公告的增删改查及状态管理接口")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @Operation(summary = "分页查询公告")
    @GetMapping("/page")
    public R getPage(@Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
                     @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
                     @Parameter(description = "标题关键词") @RequestParam(required = false) String title,
                     @Parameter(description = "分类ID") @RequestParam(required = false) Integer categoryId,
                     @Parameter(description = "是否置顶") @RequestParam(required = false) Boolean isTop,
                     @Parameter(description = "是否已发布") @RequestParam(required = false) Boolean isPublished) {
        Page<Announcement> page = new Page<>(current, size);
        return R.ok().data("page", announcementService.getPageWithCondition(page, title, categoryId, isTop, isPublished));
    }

    @Operation(summary = "获取置顶公告")
    @GetMapping("/top")
    public R getTopAnnouncements() {
        return R.ok().data("list", announcementService.getTopAnnouncements());
    }

    @Operation(summary = "设置/取消置顶")
    @PutMapping("/top/{id}")
    public R setTopStatus(@PathVariable Integer id, @RequestParam Boolean isTop) {
        return announcementService.setTopStatus(id, isTop) ? R.ok() : R.error();
    }

    @Operation(summary = "发布/撤回公告")
    @PutMapping("/publish/{id}")
    public R setPublishStatus(@PathVariable Integer id, @RequestParam Boolean isPublished) {
        return announcementService.setPublishStatus(id, isPublished) ? R.ok() : R.error();
    }

    @Operation(summary = "获取公告详情")
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return R.ok().data("announcement", announcementService.getById(id));
    }

    @Operation(summary = "新增公告")
    @PostMapping
    public R save(@RequestBody Announcement announcement) {
        return announcementService.save(announcement) ? R.ok() : R.error();
    }

    @Operation(summary = "修改公告")
    @PutMapping
    public R update(@RequestBody Announcement announcement) {
        return announcementService.updateById(announcement) ? R.ok() : R.error();
    }

    @Operation(summary = "删除公告")
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id) {
        return announcementService.removeById(id) ? R.ok() : R.error();
    }

}